﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Display.RenameUI {
    import flash.events.*;
    import Engine.Transactions.*;
    import flash.display.*;
    import Events.*;
    import flash.utils.*;
    import Display.DialogUI.*;
    import Classes.*;
    import Transactions.*;
    import Classes.util.*;
    import Display.*;
    import flash.text.*;
    import Classes.inventory.*;
    import Modules.franchise.data.*;
    import Display.aswingui.*;
    import Classes.MiniQuest.*;
    import Modules.franchise.*;
    import Modules.franchise.transactions.*;

    public class RenameDialog extends GenericDialog {

        protected var m_editingName:String;
        protected var m_editingType:String;

        public function RenameDialog(_arg1:String, _arg2:String="", _arg3:int=3, _arg4:Function=null, _arg5:String="", _arg6:String="", _arg7:Boolean=true, _arg8:int=0, _arg9:String="", _arg10:Function=null, _arg11:String=""){
            this.m_editingName = null;
            this.m_editingType = null;
            super(_arg1, _arg2, _arg3, _arg4, _arg5, _arg6, _arg7, _arg8, _arg9, _arg10, _arg11);
        }
        override protected function getAssetDependencies():Array{
            return ([DelayedAssetLoader.MARKET_ASSETS, DelayedAssetLoader.GENERIC_DIALOG_ASSETS]);
        }
        override protected function createAssetDict():Dictionary{
            var _local1:Dictionary = new Dictionary(true);
            _local1["dialog_bg"] = (new m_comObject.dialog_rename_bg() as DisplayObject);
            _local1["dialog_div"] = m_comObject.dialog_rename_divider;
            _local1["btn_up_normal"] = m_comObject.gridlist_nav_up_normal;
            _local1["btn_up_over"] = m_comObject.gridlist_nav_up_over;
            _local1["btn_up_down"] = m_comObject.gridlist_nav_up_down;
            _local1["btn_down_normal"] = m_comObject.gridlist_nav_down_normal;
            _local1["btn_down_over"] = m_comObject.gridlist_nav_down_over;
            _local1["btn_down_down"] = m_comObject.gridlist_nav_down_down;
            _local1["cell_bg"] = m_comObject.cell_bg;
            _local1["cell_bg_alt"] = m_comObject.cell_bg_alt;
            _local1["pic_cashBtn"] = EmbeddedArt.icon_cash_big;
            _local1["pic_checkGreen"] = m_comObject.checkmark_green;
            _local1["pic_dot"] = m_comObject.gridList_page_dot;
            var _local2:Object = m_assetDependencies[DelayedAssetLoader.MARKET_ASSETS];
            _local1["card_available_unselected"] = _local2.marketItem;
            _local1["card_available_selected"] = _local2.marketItem;
            _local1["icon_cash"] = _local2.cash;
            _local1["icon_coins"] = _local2.coin;
            _local1["pop_lock"] = _local2.lockedArea;
            return (_local1);
        }
        override protected function createDialogView(_arg1:Dictionary):GenericDialogView{
            var _local2:Array = this.createCityModel();
            var _local3:Array = this.createBusinessModel();
            var _local4:RenameDialogView = new RenameDialogView(_arg1, _local2, _local3, m_message, m_dialogTitle, m_type, m_callback, m_icon, m_iconPos, m_feedShareViralType, m_SkipCallback, m_customOk);
            this.refresh(_local4);
            _local4.addEventListener(RenameEvent.FINISH_CLICK, this.onFinishClick);
            _local4.addEventListener(RenameEvent.PROPOSE_CLICK, this.onProposeClick);
            _local4.addEventListener(RenameEvent.PURCHASE_CLICK, this.onPurchaseClick);
            _local4.addEventListener(RenameEvent.REQUEST_CLICK, this.onRequestClick);
            return (_local4);
        }
        protected function refresh(_arg1:RenameDialogView):void{
            _arg1.refreshCityModel(this.createCityModel());
            _arg1.refreshBusinessModel(this.createBusinessModel());
            ASwingHelper.prepare(m_jpanel);
            ASwingHelper.prepare(m_jwindow);
        }
        protected function createBusinessModel():Array{
            var _local3:OwnedFranchiseData;
            var _local4:Object;
            var _local1:Array = new Array();
            var _local2:Item = Global.gameSettings.getItemByName(RequestItemType.SIGNATURE);
            for each (_local3 in Global.franchiseManager.model.getOwnedFranchises()) {
                _local4 = new Object();
                _local4.item = _local3.franchiseType;
                _local4.itemName = Global.franchiseManager.getFranchiseName(_local3.franchiseType, Global.player.uid);
                _local4.pendingName = Global.franchiseManager.getPendingName(_local3.franchiseType, Global.player.uid);
                _local4.requiredCount = Global.franchiseManager.getSignatureCount(_local3.franchiseType);
                _local4.requiredMax = Global.franchiseManager.getRequiredSignatures(_local3.franchiseType);
                _local4.requiredCost = _local2.cash;
                _local4.state = this.computeState(_local4);
                _local4.itemName = (_local4.itemName) ? _local4.itemName : "";
                _local4.pendingName = (_local4.pendingName) ? _local4.pendingName : "";
                _local1.push(_local4);
            };
            return (_local1);
        }
        protected function createCityModel():Array{
            var _local1:Object = new Object();
            var _local2:Item = Global.gameSettings.getItemByName(RequestItemType.SIGNATURE);
            _local1.item = FranchiseManager.CITY_ITEM_NAME;
            _local1.itemName = Global.player.cityName;
            _local1.pendingName = Global.franchiseManager.getPendingName(FranchiseManager.CITY_ITEM_NAME, Global.player.uid);
            _local1.requiredCount = Global.franchiseManager.getSignatureCount(FranchiseManager.CITY_ITEM_NAME);
            _local1.requiredMax = Global.franchiseManager.getRequiredSignatures(FranchiseManager.CITY_ITEM_NAME);
            _local1.requiredCost = _local2.cash;
            _local1.state = this.computeState(_local1);
            _local1.itemName = (_local1.itemName) ? _local1.itemName : "";
            _local1.pendingName = (_local1.pendingName) ? _local1.pendingName : "";
            return ([_local1]);
        }
        protected function computeState(_arg1:Object):int{
            if (!_arg1.pendingName){
                return (RenameCell.STATE_PROPOSE);
            };
            if (_arg1.requiredCount < _arg1.requiredMax){
                return ((_arg1.state = RenameCell.STATE_REQUEST));
            };
            return ((_arg1.state = RenameCell.STATE_FINISH));
        }
        protected function onRequestClick(_arg1:RenameEvent):void{
            var _local2:String;
            var _local3:String = ZLoc.t("Items", (_arg1.itemName + "_friendlyName"));
            if (_arg1.itemName == FranchiseManager.CITY_ITEM_NAME){
                _local2 = Global.player.pendingCityName;
            } else {
                _local2 = Global.franchiseManager.getPendingName(_arg1.itemName, Global.player.uid);
            };
            if (Global.world.viralMgr.canPost("request_signature")){
                GameTransactionManager.addTransaction(new TStreamPublish("request_signature", {
                    businessFriendlyName:_local3,
                    newBusinessName:_local2
                }, Global.player.uid, {invKey:_arg1.itemName}), true, true);
                Global.world.viralMgr.postViral("request_signature");
            };
            this.refresh((m_jpanel as RenameDialogView));
        }
        protected function onProposeClick(_arg1:RenameEvent):void{
            var _local2:String;
            var _local3:String;
            var _local4:String;
            var _local5:String;
            var _local6:int;
            if (_arg1.itemName == FranchiseManager.CITY_ITEM_NAME){
                _local2 = "clerk_rename_city";
                _local3 = ZLoc.t("Dialogs", "clerk_rename_city_message", {item:ZLoc.t("Items", (_arg1.itemName + "_friendlyName"))});
                _local4 = ZLoc.t("Dialogs", "clerk_rename_city_inputLabel", {item:ZLoc.t("Items", (_arg1.itemName + "_friendlyName"))});
                _local5 = Global.player.cityName;
                _local6 = Global.gameSettings.getInt("maxCityNameLength", 22);
            } else {
                _local2 = "clerk_rename_business";
                _local3 = ZLoc.t("Dialogs", "clerk_rename_business_message", {item:ZLoc.t("Items", (_arg1.itemName + "_friendlyName"))});
                _local4 = ZLoc.t("Dialogs", "clerk_rename_business_inputLabel", {item:ZLoc.t("Items", (_arg1.itemName + "_friendlyName"))});
                _local5 = Global.franchiseManager.model.getFranchiseName(_arg1.itemName);
                _local6 = Global.gameSettings.getInt("maxBusinessNameLength", 30);
            };
            this.m_editingType = _arg1.itemName;
            var _local7:InputTextDialog = new InputTextDialog(_local3, _local2, _local4, _local5, _local6, 0, this.onNameDialogClosed);
            _local5 = ((_local5)==null) ? " " : _local5;
            this.m_editingName = _local5;
            _local7.textField.addEventListener(Event.CHANGE, this.onNameDialogChange);
            UI.displayPopup(_local7, false, "renameInputDialog");
        }
        protected function onFinishClick(_arg1:RenameEvent):void{
            if (_arg1.itemName == FranchiseManager.CITY_ITEM_NAME){
                GameTransactionManager.addTransaction(new TFinalizePendingCityName(), true);
            } else {
                GameTransactionManager.addTransaction(new TFinalizeFranchisePendingName(_arg1.itemName));
            };
            this.refresh((m_jpanel as RenameDialogView));
            if (Global.franchiseManager.getCompletedSignatureCount() < 1){
                Global.world.citySim.miniQuestManager.endMiniQuest(RequestItemMQ.QUEST_NAME);
            };
            closeMe(null);
        }
        protected function onPurchaseClick(_arg1:RenameEvent):void{
            var _local2:Item = Global.gameSettings.getItemByName(RequestItemType.SIGNATURE);
            if (((_local2) && ((Global.player.cash >= _local2.cash)))){
                GameTransactionManager.addTransaction(new TBuyRequestItem(RequestItemType.SIGNATURE, _arg1.itemName));
                Global.player.cash = (Global.player.cash - _local2.cash);
            };
            this.refresh((m_jpanel as RenameDialogView));
        }
        protected function onNameDialogChange(_arg1:Event):void{
            var _local2:TextField = (_arg1.target as TextField);
            if (_local2){
                this.m_editingName = _local2.text;
            };
        }
        protected function onNameDialogClosed(_arg1:GenericPopupEvent):void{
            if (_arg1.button == GenericDialogView.YES){
                if (this.m_editingType == FranchiseManager.CITY_ITEM_NAME){
                    GameTransactionManager.addTransaction(new TUpdatePendingCityName(this.m_editingName), true);
                } else {
                    GameTransactionManager.addTransaction(new TUpdateFranchisePendingName(this.m_editingType, this.m_editingName), true);
                };
                this.m_editingName = null;
                this.m_editingType = null;
                this.refresh((m_jpanel as RenameDialogView));
            };
        }
        public function updateDataFormServer():void{
            this.refresh((m_jpanel as RenameDialogView));
        }
        override public function close():void{
            super.close();
            Global.ui.renameDialog = null;
        }

    }
}//package Display.RenameUI 
